<?php

class AdminPagesController extends AppController
{
    public $helpers = array('Html', 'Form');

    public $components = array(
        'RequestHandler',
        'Session',
        'Auth'
    );

    var $uses = array(
        'Page',
        'Category'
    );

    public function beforeFilter()
    {
        parent::beforeFilter();
        $this->Auth->loginAction = array(
            'plugin' => 'users',
            'controller' => 'users',
            'action' => 'login',
            'admin' => false
        );
        $this->Auth->loginRedirect = '/';
        $this->Auth->logoutRedirect = '/';
        $this->Auth->authError = __('Sorry, but you need to login to access this location.', true);
        $this->Auth->loginError = __(
            'Invalid e-mail / password
                    combination.  Please try again',
            true
        );
        $this->Auth->autoRedirect = false;
        $this->Auth->userModel = 'User';
        $this->Auth->userScope = array('User.active' => 1, 'User.is_admin' => '1');
        if ($this->Auth->user()) {
            $this->set('userData', $this->Auth->user());
            $this->set('isAuthorized', ($this->Auth->user('id') != ''));
        }
        $this->layout = 'admin';
    }

    public function index()
    {
        $this->set('pages', $this->Page->find('all'));
    }

    public function view($id = null)
    {
        if (!$id) {
            throw new NotFoundException(__('Invalid page'));
        }

        $page = $this->Page->findById($id);
        if (!$page) {
            throw new NotFoundException(__('Invalid page'));
        }
        $this->set('page', $page);
    }

    public function add()
    {
        if ($this->request->is('post')) {
            $this->Page->create();
            if ($this->Page->save($this->request->data)) {
                $this->Session->setFlash(__('Your page has been saved.'), 'default', array(), 'success');

                return $this->redirect(array('action' => 'index'));
            }
            $this->Session->setFlash(__('Unable to add your page.'), 'default', array(), 'error');
        }

        $images = array();
        $page_root = $_SERVER['DOCUMENT_ROOT'] . '/images/pages';
        if ($handle = opendir($page_root)) {
            while (false !== ($entry = readdir($handle))) {
                if ($entry != "." && $entry != "..") {
                    $images[] = '/images/pages/' . $entry;
                }
            }
            closedir($handle);
        }
        $this->set('images', $images);

        $categories = array();
        $conditions = array("Category.id_parent" => null);
        foreach ($this->Category->find(
                     'all',
                     array('conditions' => $conditions, 'order' => array('Category.id' => 'asc'))
                 ) as $category) {
            $categories[$category['Category']['id']] = $category['Category']['name'];
        }

        $this->set('categories', $categories);
    }

    public function edit($id = null)
    {
        if (!$id) {
            throw new NotFoundException(__('Invalid page'));
        }
        $page = $this->Page->findById($id);

        if (!$page) {
            throw new NotFoundException(__('Invalid Page'));
        }

        if ($this->request->is(array('post', 'put'))) {
            $this->Page->id = $id;
            if ($this->Page->save($this->request->data)) {
                $this->Session->setFlash(__('Your Page has been updated.'));

                return $this->redirect(array('action' => 'index'));
            }
            $this->Session->setFlash(__('Unable to update your Page.'));
        }

        if (!$this->request->data) {
            $categories = array();
            $conditions = array("Category.id_parent" => null);
            foreach ($this->Category->find(
                         'all',
                         array('conditions' => $conditions, 'order' => array('Category.id' => 'asc'))
                     ) as $category) {
                $categories[$category['Category']['id']] = $category['Category']['name'];
            }
            $this->set('categories', $categories);
            $category = $this->Category->findById($page['Page']['id_cat']);

            $images = array();
            $page_root = $_SERVER['DOCUMENT_ROOT'] . '/images/pages';
            if ($handle = opendir($page_root)) {
                while (false !== ($entry = readdir($handle))) {
                    if ($entry != "." && $entry != "..") {
                        $images[] = '/images/pages/' . $entry;
                    }
                }
                closedir($handle);
            }
            $this->set('images', $images);

            $sub_cats = array();
            if (isset($category['Children'])) {
                foreach ($category['Children'] as $sub_cat) {
                    $sub_cats[$sub_cat['id']] = $sub_cat['name'];
                }
            }

            $this->set('sub_categories', $sub_cats);
            $this->request->data = $page;
        }
    }

    public function delete($id)
    {
        if ($this->request->is('get')) {
            throw new MethodNotAllowedException();
        }
        if ($this->Page->delete($id)) {
            $this->Session->setFlash(__('The Page with id: %s has been deleted.', h($id)));

            return $this->redirect(array('action' => 'index'));
        }
    }

    public function getSubCat()
    {
        $id_cat = $this->request->data('id_cat');
        if (!$id_cat) {
            throw new NotFoundException(__('Invalid category'));
        }

        $category = $this->Category->findById($id_cat);
        if (!$category) {
            throw new NotFoundException(__('Invalid category'));
        }
        $subcats = $category['Children'];
        $this->set('subcats', $subcats);
        $this->set('_serialize', array('subcats'));
    }

    public function changeGrid($idpage)
    {
        echo $idpage;
        $this->Page->read(null, $idpage);
        $grid = $this->request->data['grid'];
        $this->Page->set('grid', $grid);
        $this->Page->save();
        $this->render(false);

        return 'OK';
    }

}

?>